package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.UserBanned;
import com.yunkeji.dao.mapper.UserBannedMapper;
import com.yunkeji.dao.vo.UserBannedVo;
import com.yunkeji.system.domain.bo.UserBannedBo;
import com.yunkeji.system.service.IUserBannedService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 封号信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class UserBannedServiceImpl implements IUserBannedService {

    private final UserBannedMapper baseMapper;

    /** 查询封号信息 */
    @Override
    public UserBannedVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询封号信息列表 */
    @Override
    public TableDataInfo<UserBannedVo> queryPageList(UserBannedBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserBanned> lqw = buildQueryWrapper(bo);
//        Page<UserBannedVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        Page<UserBannedVo> result = baseMapper.selectListPage(
            bo.getPhoneNum(),
            bo.getPlatformId(),
            bo.getLimitType(),
            pageQuery.build());
        return TableDataInfo.build(result);
    }

    /** 查询封号信息列表 */
    @Override
    public List<UserBannedVo> queryList(UserBannedBo bo) {
        LambdaQueryWrapper<UserBanned> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserBanned> buildQueryWrapper(UserBannedBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserBanned> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserBanned::getUserId, bo.getUserId());
        lqw.eq(bo.getPlatformId() != null, UserBanned::getPlatformId, bo.getPlatformId());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), UserBanned::getReason, bo.getReason());
        lqw.eq(bo.getEndTime() != null, UserBanned::getEndTime, bo.getEndTime());
        lqw.eq(bo.getLimitType() != null, UserBanned::getLimitType, bo.getLimitType());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNum()), UserBanned::getPhoneNum, bo.getPhoneNum());
        lqw.eq(bo.getRemark() != null, UserBanned::getRemark, bo.getRemark());
        return lqw;
    }

    /** 新增封号信息 */
    @Override
    public Boolean insertByBo(UserBannedBo bo) {
        UserBanned add = BeanUtil.toBean(bo, UserBanned.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改封号信息 */
    @Override
    public Boolean updateByBo(UserBannedBo bo) {
        UserBanned update = BeanUtil.toBean(bo, UserBanned.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(UserBanned entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除封号信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
